﻿

/**
*      
*/
Chenxin.UI.AllothouseForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/allotHouseMangager.ashx',
    id: 'allothouseWin',
    renderTo: 'jqm_allothouse',
	jqmWidth:700,
	getListOp:'gethouselist',
	listAltMsg:'请选择房源！',
	getAlltCustomerOp:'getallotusers',
	defBBtn:true,
	reset:function(){
		Chenxin.UI.AllothouseForm.superclass.reset.call(this);
		
		$('#tr_methed').show();
		$('#tr_date').hide();
		$('#tr_list').hide();
		if(this.houseList){
			this.houseList.grid.clear();
		}
	},
	saveSucc:function(){
		//this.form.findByName('allotck').attr('checked',false);
		//debugger;
		this.form.houseList.grid.reload();
		this.grid.reload();
		//$('#checkall').attr('checked',false)
	},
	afterGetFormJson:function(json){
		if(json.startDay){
			delete json.startDay;
		}
		if(json.endDay){
			delete json.endDay;
		}
		if(json.checkall){
			delete json.checkall;
        }
        if (json.keyword) {
            delete json.keyword;
        }
		
		this.form.additionalParam.allotStyle=json.allotStyle;
		if(json.allotStyle=='10'){//手动分配，需要传方法,不传房源ID列表
			this.form.additionalParam.allotType=json.allotType;	
			if(this.form.additionalParam.houseIdList){
				delete this.form.additionalParam.houseIdList
			}
			
			this.form.autoHide=true;//提交成功，自动关闭层
			
			
			//控制，当选择快速分配和分配给人时，需要选择人，否则，需要选择部门
			if(json.allotType=='10'){
				this.form.findByName('NowPersonID').required=true;
				this.form.findByName('NowDepartID').required=false;
				
				this.form.autoHide=true;//提交成功，自动关闭层
			}else{
				
				this.form.findByName('NowPersonID').required=false;
				this.form.findByName('NowDepartID').required=true;
				
			}
			
		}else{
			this.form.additionalParam.houseIdList=json.allotck;
			if(this.form.additionalParam.allotType){
				delete this.form.additionalParam.allotType;
			}
			
			this.form.autoHide=false;//提交成功，不自动关闭层
				
			//选择手动分配，需要选择到人
			this.form.findByName('NowPersonID').required=true;
			this.form.findByName('NowDepartID').required=false;
		}
		delete json.allotStyle;
		delete json.allotType;	
		if(json.allotck){
			delete json.allotck;
		}
		
		
	},
	createPage:function(){
		return new Chenxin.UI.AllothousePage({
			actionUrl:this.actionUrl + '?op='+this.getListOp
		});
	},
	getAlloOpType:function(){
		var _this=this;
		Chenxin.ajax({
			url:this.actionUrl+'?op=getallooptype',			
			type: "get",
			dataType: "json",
			onSuccess:function(data,msg){
				_this.findByName('OldPersonID').required=data.byuser;
			}
		});
	},
    createForm:function() {
        return Chenxin.FormCommon.allotHouseMangagerForm(this.id);
    },
    customerFormVal:function(model){
		if(this.additionalParam.allotStyle==20&&!this.additionalParam.houseIdList){
			alert(this.listAltMsg);
			return false;
		}
		return true;
	},
    createComboList: function() {
        
		var op_city=new Chenxin.UI.StructCombo({
			param:{op:'getcity'},
			containerId:'#'+this.id+'_op_city',
			name:'OldCityId',
			inModelType:'all',
			txtModelName:'OldCity',
			tbxWidth: 60,
			required:true,
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.initValue=this.items[0].val;
					op_company.param.fatherid=this.items[0].val;
					op_company.reload();
				}else{
					this.addPleaseSelected=true;			
				}
			},
			onSelected:function(){
				if(this.getValue()==this.defVal){
					op_company.clearItems();
				}else{
					op_company.param.fatherid=this.getValue();
					op_company.reload();
				}
				op_departs.clearItems();
				op_emp.clearItems();
			}
		});
		
		//公司
		var op_company=new Chenxin.UI.StructCombo({
		    autoLoad: false,
			param:{op:'getcompany'},
			tbxWidth:60,
			name:'OldCompanyId',
			inModelType:'all',
			txtModelName:'OldCompany',
			containerId:'#'+this.id+'_op_company',
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.initValue=this.items[0].val;
					op_departs.param.fatherid=this.items[0].val;
					op_departs.reload();
				}else{
					this.addPleaseSelected=true;					
				}
			},
			onSelected:function(){
				if(this.getValue()==this.defVal){
					op_departs.clearItems();
				}else{
					op_departs.param.fatherid=this.getValue();
					op_departs.reload();
				}
				op_emp.clearItems();
			}
		});
		//门店
		var op_departs=new Chenxin.UI.StructCombo({
			autoLoad:false,
			tbxWidth: 60,
			colWidth: 100,
			hasSearch:true,
			name:'OldDepartID',
			inModelType:'all',
			txtModelName:'OldDepart',
			containerId:'#'+this.id+'_op_depart',
			param: { op: 'getallotdeparts' },
			hasPage: true,
			pageSize:10,
			required: true,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					op_emp.clearItems();
				}else{
					op_emp.param.fatherid=this.getValue();
					op_emp.reload();
				}
			}
		});
		//人
		var op_emp=new Chenxin.UI.StructCombo({
			autoLoad:false,
			tbxWidth: 60,
			colWidth: 120,
			name:'OldPersonID',
			inModelType:'all',
			txtModelName:'OldPerson',
			containerId:'#'+this.id+'_op_emp',
			//required:true,
			param:{op:this.getAlltCustomerOp}
		});
		
		var aim_city=new Chenxin.UI.StructCombo({
			param:{op:'getcity'},
			containerId:'#'+this.id+'_aim_city',
			name:'NowCityId',
			inModelType:'all',
			txtModelName: 'NowCity',
			required: true,
			tbxWidth:60,
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.initValue=this.items[0].val;
					aim_company.param.fatherid=this.items[0].val;
					aim_company.reload();
				}else{
					this.addPleaseSelected=true;			
				}
			},
			onSelected:function(){
				if(this.getValue()==this.defVal){
					aim_company.clearItems();
				}else{
					aim_company.param.fatherid=this.getValue();
					aim_company.reload();
				}
				aim_departs.clearItems();
				aim_emp.clearItems();
			}
		});
		
		//公司
		var aim_company=new Chenxin.UI.StructCombo({
			autoLoad:false,
			param:{op:'getcompany'},
			tbxWidth:60,
			name:'NowCompanyId',
			inModelType:'all',
			txtModelName:'NowCompany',
			containerId:'#'+this.id+'_aim_company',
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.initValue=this.items[0].val;
					aim_departs.param.fatherid=this.items[0].val;
					aim_departs.reload();
				}else{
					this.addPleaseSelected=true;					
				}
			},
			onSelected:function(){
				if(this.getValue()==this.defVal){
					aim_departs.clearItems();
				}else{
					aim_departs.param.fatherid=this.getValue();
					aim_departs.reload();
				}
				aim_emp.clearItems();
			}
		});
		//门店
		var aim_departs=new Chenxin.UI.StructCombo({
			autoLoad:false,
			tbxWidth: 60,
			colWidth: 100,
			hasSearch: true,
			name:'NowDepartID',
			inModelType:'all',
			txtModelName:'NowDepart',
			containerId:'#'+this.id+'_aim_depart',
			param: { op: 'getallotdeparts' },
			hasPage: true,
			pageSize: 10,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					aim_emp.clearItems();
				}else{
					aim_emp.param.fatherid=this.getValue();
					aim_emp.reload();
				}
			}
		});
		//人
		var aim_emp=new Chenxin.UI.StructCombo({
			autoLoad:false,
			tbxWidth: 60,
			colWidth:120,
			name:'NowPersonID',
			inModelType:'all',
			txtModelName:'NowPerson',
			containerId:'#'+this.id+'_aim_emp',
			required:true,
			param:{op:'getemployee',usertype:1}
		});
		
        return [op_city, op_company, op_departs, op_emp,aim_city, aim_company, aim_departs, aim_emp];
    },
    initEvent: function() {
        var _this = this;
		this.findByName('allotStyle').live('click',function(){
			if(this.value=='10'){
				$('#tr_methed').show();
				$('#tr_date').hide();
				$('#tr_list').hide();
			}else{
				$('#tr_methed').hide();
				$('#tr_date').show();
				$('#tr_list').show();				
			}
		});
		
		
		$('#allothouseWin_Save').live('click',function(){
			_this.save();
		});
		$('#allothouseWin_Cancle').live('click',function(){
			_this.hide();
		});
		$('#checkall').live('click',function(){
			_this.findByName('allotck').attr('checked',this.checked);
		});
    },
    initComponent: function() {
        Chenxin.UI.AllothouseForm.superclass.initComponent.call(this);
		this.houseList=this.createPage();
		this.houseList.search.comboList.push(this.findByName('OldCityId'),this.findByName('OldCompanyId'),this.findByName('OldDepartID'),this.findByName('OldPersonID'));
		this.getAlloOpType();
    }
});

Chenxin.UI.AllothousePage=Chenxin.extend(Chenxin.UI.PageCommon,{
    autoInitForm: false,
	createGrid:function(){
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl,
            pageSize: 100,
			autoLoad:false,
			renderTo:'house_list'
        });	
	},
	createSearch:function(){
		return new Chenxin.UI.AllothouseSearch({
			scope:this
		});
	},
	initComponent:function(){
		Chenxin.UI.AllothousePage.superclass.initComponent.call(this);
		//this.getAlloOpType();
	}
});

Chenxin.UI.AllothouseSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
	id:'allothouseWin_search',
	afterGetParamJson:function(model){
		if(model.startDay&&model.endDay){
			if(Chenxin.Fn.compareDate(model.startDay,model.endDay)){				
				alert('开始日期不能大于结束日期！');
				this.autoReload=false;
				return ;
			}
		}
	
		if(!model.OldPersonID){
			alert('请选择员工！');
			this.autoReload=false;
			return;
		}
		this.autoReload=true;
	},
	initEvent:function(){
		var _this=this;
		$('#allothouseWin_btn_search').live('click',function(){
			_this.scope.reloadGrid();
		});
	},
	initComponent:function(){
		Chenxin.UI.AllothouseSearch.superclass.initComponent.call(this);
	}
});

/**
*房源分配表单
*/
Chenxin.FormCommon.allotHouseMangagerForm = function(comboIdPrefix) {
    //return '<table class="tbl_f"><tbody><tr><th>分配方式：</th><td><input type="radio" name="allotStyle" value="10" checked="checked"/>快速  <input type="radio" name="allotStyle" value="20" />手动</td></tr><tr><td colspan="2"><table style="width:100%;" id="' + comboIdPrefix + '_search"><tr><th>被分配的对象：</th><td><div class="fl">城市</div><div style="z-index:911; margin-right: 5px;" id="' + comboIdPrefix + '_op_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:910; margin-right: 5px;" id="' + comboIdPrefix + '_op_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:909; margin-right: 5px;" id="' + comboIdPrefix + '_op_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:908; margin-right: 5px;" id="' + comboIdPrefix + '_op_emp" class="SelectWrapSingle"></div></td></tr><tr id="tr_date" style="display:none"><th>建档日期：</th><td><input style="width:115px;" name="startDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />  至  <input style="width:115px;" name="endDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" /> <input id="' + comboIdPrefix + '_btn_search" type="button" value="" class="btn_s_0" /></td></tr></table></td></tr><tr id="tr_methed"><th>分配方法：</th><td><input type="radio" name="allotType" value="10" checked="checked"/>全部分配给人<input type="radio" name="allotType" value="20" />全部分配给店<input type="radio" name="allotType" value="30" />平均分配给店</td></tr><tr id="tr_list" style="display:none"><td colspan="2"><div id="house_list" style=\"height:300px;\"></div></td></tr><tr><th>接收的对象：</th><td><div class="fl">城市</div><div style="z-index:907; margin-right: 5px;" id="' + comboIdPrefix + '_aim_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:906; margin-right: 5px;" id="' + comboIdPrefix + '_aim_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:905; margin-right: 5px;" id="' + comboIdPrefix + '_aim_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:904; margin-right: 5px;" id="' + comboIdPrefix + '_aim_emp" class="SelectWrapSingle"></div></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></tbody></table>';
    return '<table class="tbl_f"><tbody><tr><td>分配<input type="radio" name="allotStyle" value="10" checked="checked"/>快速  <input type="radio" name="allotStyle" value="20" />手动</td><td><div class="fl">城市</div><div style="z-index:911; margin-right: 5px;" id="' + comboIdPrefix + '_op_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:910; margin-right: 5px;" id="' + comboIdPrefix + '_op_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:909; margin-right: 5px;" id="' + comboIdPrefix + '_op_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:908; margin-right: 5px;" id="' + comboIdPrefix + '_op_emp" class="SelectWrapSingle"></div></td></tr><tr><td colspan="2"><table style="width:100%;" id="' + comboIdPrefix + '_search"><tr id="tr_date" style="display:none"><th>建档日期：</th><td><input style="width:115px;" name="startDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />  至  <input style="width:115px;" name="endDay"  class="inputbox_n" type="text" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});"  readonly="readonly" />&nbsp;&nbsp;&nbsp;&nbsp;关键字：<input id="keyword" name="keyword" class="inputbox_n" type="text" style="width:150px;" /> <input id="' + comboIdPrefix + '_btn_search" type="button" value="" class="btn_s_0" /></td></tr></table></td></tr><tr id="tr_methed"><th>分配方法：</th><td><input type="radio" name="allotType" value="10" checked="checked"/>全部分配给人<input type="radio" name="allotType" value="20" />全部分配给店<input type="radio" name="allotType" value="30" />平均分配给店</td></tr><tr id="tr_list" style="display:none"><td colspan="2"><div id="house_list" style=\"height:200px;\"></div></td></tr><tr><th>接收的对象：</th><td><div class="fl">城市</div><div style="z-index:907; margin-right: 5px;" id="' + comboIdPrefix + '_aim_city" class="SelectWrapSingle"></div><div class="fl">公司</div><div style="z-index:906; margin-right: 5px;" id="' + comboIdPrefix + '_aim_company" class="SelectWrapSingle"></div><div class="fl">部门</div><div style="z-index:905; margin-right: 5px;" id="' + comboIdPrefix + '_aim_depart" class="SelectWrapSingle"></div><div class="fl">人员</div><div style="z-index:904; margin-right: 5px;" id="' + comboIdPrefix + '_aim_emp" class="SelectWrapSingle"></div></td></tr></tbody></table>';
};//<tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr>
